//
//  MyQianBaoViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MyQianBaoViewController.h"
#import "QianBaoTableViewCell.h"
#import "ChongZhiViewController.h"

@interface MyQianBaoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UIView *headerView;
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UIButton *chongZhiBtn;

@property (nonatomic, strong)NSMutableArray *dataArray;

@end

@implementation MyQianBaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的钱包";
    
    [self setHeaderView];
    
    [self loadChongZhi];
    
    [self addTableView];
    
    [self.tableView registerClass:[QianBaoTableViewCell class] forCellReuseIdentifier:@"QianBaoTableViewCell"];

}

-(void)loadChongZhi{
    
    _dataArray = [NSMutableArray array];
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Course&a=recherge",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            for (NSDictionary *dicc in responseObject[@"data"][@"recherge_info"]) {
                [_dataArray addObject:dicc];
                
            }
            
            if (_dataArray.count == 0) {
                [MBProgressHUD showTextOnly:self.view message:@"暂无充值记录" delay:2.0];
            }
            
            [self.tableView reloadData];
            
        }else{
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)setHeaderView{
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, 135 * KscreenHeight)];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10 * KscreenWidth, 10 * KscreenHeight, 100 * KscreenWidth, 20 * KscreenHeight)];
    lable.text = @"我的学豆";
    [_headerView addSubview:lable];
    
    UIImageView *imageVC = [[UIImageView alloc]initWithFrame:CGRectMake(10 * KscreenWidth, 50 * KscreenHeight, 30 * KscreenWidth, 30 * KscreenHeight)];
    [imageVC setImage:[UIImage imageNamed:@"积分-1"]];
    [_headerView addSubview:imageVC];
    
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(50 * KscreenWidth, 55 * KscreenHeight, 100 * KscreenWidth, 20 * KscreenHeight)];
    lable1.text = [ZYUserManager userInfo].left_xuedou;
    lable1.textColor = [UIColor redColor];
    [_headerView addSubview:lable1];
    
    _chongZhiBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _chongZhiBtn.frame = CGRectMake(ScreenWidth - 80 * KscreenWidth, 50 * KscreenHeight, 60 * KscreenWidth, 30 * KscreenHeight);
    _chongZhiBtn.backgroundColor = [UIColor redColor];
    [_chongZhiBtn setTitle:@"充值" forState: 0];
    [_chongZhiBtn addTarget:self action:@selector(chongzhiAction) forControlEvents:(UIControlEventTouchUpInside)];
    _chongZhiBtn.layer.masksToBounds = YES;
    _chongZhiBtn.layer.cornerRadius = 3 * KscreenWidth;
    [_headerView addSubview:_chongZhiBtn];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake( 0, 95 * KscreenHeight, ScreenWidth, 0.5 * KscreenHeight)];
    line.backgroundColor = hui4Color;
    [_headerView addSubview:line];
    
    UILabel *jilulable = [[UILabel alloc]initWithFrame:CGRectMake(10 * KscreenWidth, 105 * KscreenHeight, 100 * KscreenWidth, 20 * KscreenHeight)];
    jilulable.text = @"充值记录";
    [_headerView addSubview:jilulable];
}

//充值事件
-(void)chongzhiAction{
    ChongZhiViewController *chongHZiVC = [[ChongZhiViewController alloc]init];
    [self.navigationController pushViewController:chongHZiVC animated:YES];
    
}

-(void)addTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT ) style:UITableViewStylePlain];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.delegate        = self;
    _tableView.dataSource      = self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    _tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    _tableView.tableHeaderView = _headerView;
    //    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //tableView有数据时才显示分割线
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    
    [self.view addSubview:_tableView];
}



#pragma mark - Navigation
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    return 0;
    
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifity = @"QianBaoTableViewCell";
    //    MyIndentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifity];
    QianBaoTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[QianBaoTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifity];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if (indexPath.row == 0) {
        cell.timeLable.text = @"时间";
        cell.xuedouLable.text = @"学豆";
        cell.backgroundColor = hui2Color;
    }else{
        
        cell.timeLable.text = [NSString stringWithFormat:@"%@",[_dataArray[indexPath.row - 1] valueForKey:@"create_time"]];
        float xuedou = [[_dataArray[indexPath.row - 1] valueForKey:@"xuedou_num"] floatValue];
        cell.xuedouLable.text = [NSString stringWithFormat:@"%.2f",xuedou/100];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40 * KscreenHeight;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    NSLog(@"%zd",indexPath.row);
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
